<template>

  <div class="app-container" style="font-size:12px">
    <div class="filter-container">
      <el-date-picker v-model="listQuery.date1" style="width:150px;" format="yyyy-MM-dd" type="datetime" placeholder="租车开始日期" />
      <el-date-picker v-model="listQuery.date2" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="租车结束日期" />
      <el-input v-model="listQuery.customer" placeholder="客户姓名" style="width: 100px;margin-top:6px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.c_id" placeholder="选择门店" filterable class="filter-item" style="width:150px;margin-top:8px">
        <el-option label="选择门店" value="" />
        <el-option
          v-for="item in company_list"
          :key="item.id"
          :label="item.c_name"
          :value="item.id"
        />
      </el-select>
      <onwerCompany ref="owner_company" />
      <el-select v-model="listQuery.shareholder" placeholder="股东" class="filter-item" style="width:130px;margin-top:8px">
        <el-option label="股东" value="" />
        <el-option
          v-for="item in shareholderList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:6px" @click="handleFilter">查询</el-button>
      <excelout />
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :element-loading-text="loadingtext" :data="list" size="mini" :row-class-name="tableRowClassName" border fit highlight-current-row style="width: 100%;margin-top:20px">
      <el-table-column label="编号" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.order_contract_num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开支" align="center">
        <template slot-scope="{row}">
          <span>{{ row.outcome_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租车日期" align="center" width="100px">
        <template slot-scope="{row}">
          {{ row.create_time }}
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center">
        <template slot-scope="{row}">
          <span style="color:dodgerblue;cursor: pointer" @click="toPayment(row.order_id)">{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="首期付租日期" align="center" width="100px">
        <template slot-scope="{row}">
          {{ row.first_payment_day }}
        </template>
      </el-table-column>
      <el-table-column label="杂费" align="center">
        <template slot-scope="{row}">
          <span>{{ row.incidental_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租金" align="center">
        <template slot-scope="{row}">
          <span>{{ row.a_month_price }}</span>
        </template>
      </el-table-column>

      <el-table-column label="期数" align="center" width="70px">
        <template slot-scope="{row}">
          <span>{{ row.total_period }}</span>
        </template>
      </el-table-column>
      <el-table-column label="融资金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.total_price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务类型" align="center">
        <template slot-scope="{row}">
          <span>{{ row.goods_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属员" align="center" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.c_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="股东出资" align="center" width="150">
        <template slot-scope="{row}">
          <span v-for="items in row.shareholder_count">{{ items }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总部出资" align="center">
        <template slot-scope="{row}">
          <span>{{ row.headquarters }}</span>
        </template>
      </el-table-column>

      <el-table-column label="业务人员" align="center">
        <template slot-scope="{row}">
          <span>{{ row.sale_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出资公司" align="center">
        <template slot-scope="{row}">
          <span>{{ row.company_name }}</span>
        </template>
      </el-table-column>

    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>

</template>

<script>
import excel from '@/components/excelout/index.vue'

import {
  financeOrderList, exceloutfinance, returnOrderSubmit, serverAllMoneySubmit, orderShareHolderSet, getShareHolder, exceloutfinanceCz
} from '@/api/finance'
import { formatDate } from '@/utils/formatDate.js'
import { merchantListOnlyMerchant } from '@/api/merchant'
import excelout from '@/components/excelout/index.vue'
import waves from '@/directive/waves' // waves directive
import onwerCompany from '@/components/company/owner_company.vue'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {

    Pagination, excelout, onwerCompany

  },
  directives: {
    waves
  },

  filters: {
   	formatDate(time) {
   	        time = time * 1000
   	        const date = new Date(time)

   	        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
   	      }
  },
  data() {
    return {
      shareQuery: {
        limit: 100
      },
      loadingtext: '数据加载中...',
      shareholderList: [],
      other_form: {
        order_id: '',
        money: '',
        desc: '',
        incidental_money: '',
        outcome_money: ''
      },
      shareholder_from: {
        order_id: '',
        datas: [
          {
            shareholder: '',
            money: '',
            percentage: '',
            benefits: ''
          }
        ]
      },

      tableKey: 0,
      shareholderAble: false,
      descAble: false,
      outcomeInputAble: false,
      incidentalAble: false,
      tableRecord: 1,
      list: null,
      record_list: null,
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        customer: '',
        c_id: '',
        sale_id: '',
        order_status: '',
        excel: 0,
        flag: 1,
        date1: '',
        date2: '',
        shareholder: ''
      },
      listMerchantQuery: {
        page: 1,
        limit: 500
      },
      company_list: [],
      sale_list: [],
      form: []
    }
  },
  created() {
    this.cacheQueryFormat()

    this.getShareHolder()
    this.listQuery.shareholder = this.$route.query.shareholder
    this.getCompanyList()
    this.getList()
    // 添加钩子，页面刷新
    window.addEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  destroyed() {
    // 添加钩子，页面刷新
    window.removeEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  methods: {
    addDocument() {
      const o = { shareholder: '', money: '', percentage: '' }
      this.shareholder_from.datas.push(o)
    },
    getShareHolder() {
      getShareHolder(this.shareQuery).then(response => {
        this.shareholderList = response.data.data
      })
    },
    delDocument(k) {
      this.shareholder_from.datas.map((item, index) => {
        if (index == k) {
          this.shareholder_from.datas.splice(index, 1)
        }
      })
    },
    beforeunloadFn(e) {
      // 刷新页面事件
      sessionStorage.removeItem('financeList')
    },
    cacheQueryFormat() {
      const cacheQuery = sessionStorage.getItem('financeList')
      if (cacheQuery) {
        this.listQuery = JSON.parse(cacheQuery)
      }
    },
    getList() {
      sessionStorage.setItem('financeList', JSON.stringify(this.listQuery))
      financeOrderList(this.listQuery).then(response => {
        if (!this.listQuery.excel) {
          this.list = response.data.list.data
          this.total = response.data.list.total
          // this.allcount = response.data.countall

          setTimeout(() => {
            this.listLoading = false
          }, 0.1 * 1000)
        } else {
          this.listQuery.excel = 0
        }
      })
    },
    outcomeInput(order_id, row) {
      this.outcomeInputAble = true
      this.other_form.order_id = order_id
      this.other_form.outcome_money = row.outcome_money
    },
    shareholder(order_id, row) {
      this.shareholderAble = true
      this.shareholder_from.order_id = order_id
      this.getShareHolder()
    },
    incidental(order_id, row) {
      this.incidentalAble = true
      this.other_form.order_id = order_id
      this.other_form.incidental_money = row.incidental_money
    },
    desc(order_id, row) {
      this.descAble = true
      this.other_form.order_id = order_id
      this.other_form.desc = row.descript
    },
    returnOrderSubmit() {
      returnOrderSubmit(this.form).then(response => {
        this.$notify({
          title: '提示',
          message: '修改成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
        this.returnOrderAble = false
      })
    },

    orderShareHolderSet() {
      orderShareHolderSet(this.shareholder_from).then(response => {
        if (response.code == 200) {
          this.$notify({
            title: '提示',
            message: response.msg,
            type: 'success',
            duration: 2000,
            onClose: this.getList()
          })
          this.shareholderAble = false
        } else {
          this.$notify({
            title: '提示',
            message: response.msg,
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    serverAllMoneySubmit(lab) {
      this.other_form.lab = lab
      serverAllMoneySubmit(this.other_form).then(response => {
        this.$notify({
          title: '提示',
          message: '添加成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
        this.outcomeInputAble = false
        this.incidentalAble = false
        this.descAble = false
      })
    },
    handleFilter() {
      this.listLoading = true
      const owner_company_id = this.$refs.owner_company.$data.listQuery.company_id
      this.listQuery.owner_company_id = owner_company_id
      this.listQuery.page = 1
      this.getList()
    },

    excelout() {
      this.listLoading = true
      this.loadingtext = '正在生成表格，请稍等...'
      this.listQuery.page = 1
      this.listQuery.limit = this.total
      exceloutfinanceCz(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },

    getCompanyList() {
      merchantListOnlyMerchant(this.listMerchantQuery).then(response => {
        this.company_list = response.data.company
        this.sale_list = response.data.sale.data
      })
    },
    toPayment(order_id) {
      this.$router.push('/finance/customerpayment/' + order_id)
    },

    handleRecord(order_id) {
      console.log(order_id)
      this.dialogStatus = 'record'
      this.dialogRecordVisible = true
      this.listRecordQuery.order_id = order_id
      fetchOrderRecordListByOrderId(this.listRecordQuery).then(res => {
        this.record_list = res.data.list
        this.recordTotal = res.data.count
        setTimeout(() => {
          this.listRecordLoading = false
        }, 1.0 * 1000)
      })
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.s == 1) {
        return 'success-row'
      } else if (row.s == 2) {
        return 'warning-row'
      } else if (row.s == 3) {
        return 'default-row'
      }
      return ''
    }

  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  td{padding: 0;}
  select{
    width:100px;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    float: left;
  }
   .el-table .warning-row {
      background: orangered;
    }

  .el-table .success-row {
    background: #98dd72;
  }
  .el-table .default-row {
    background: #ccc;
  }
</style>
